package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Consumption;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {
    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;



    @RequiredPermission(name="结算单页面",expression="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionQueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("business", businessService.listAll());
        model.addAttribute("consumptionStatusEnum", ConsumptionStatusEnum.values());
        return "consumption/list";
    }

    @RequiredPermission(name="结算单删除",expression="consumption:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        if (id != null) {
           consumptionService.delete(id);
        }
        return new JsonResult();
    }


    @RequiredPermission(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {
        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/input?id=" + consumption.getId();
    }


    @RequiredPermission(name="结算单新增/编辑跳转",expression="consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("consumption",consumptionService.get(id));
            model.addAttribute("consumptionItemList", consumptionService.queryByConsumptionItemItem(id));
        }
        model.addAttribute("businessList", businessService.listAll());
        model.addAttribute("parentList", systemDictionaryItemService.selectParentList("business"));
        model.addAttribute("payTypeList", systemDictionaryItemService.selectParentList("pay_type"));
        return "consumption/input";
    }
    @RequiredPermission(name="结算单新增/编辑",expression="consumption:saveAppointment")
    @RequestMapping("/saveAppointment")
    public String saveAppointment(Long aid) {
        Long id = consumptionService.saveAppointment(aid);
        return "redirect:/consumption/input?id=" + id;
    }


    @RequiredPermission(name="结算单结算",expression="consumption:consume")
    @RequestMapping("/consume")
    @ResponseBody
    public JsonResult consume(Consumption consumption) {
        consumptionService.consume(consumption);
        return new JsonResult();
    }

    @RequiredPermission(name="结算单审核",expression="consumption:audit")
    @RequestMapping("/audit")
    @ResponseBody
    public JsonResult audit(Consumption consumption) {
        consumptionService.audit(consumption);
        return new JsonResult();
    }
}
